﻿Public Class NumPad
  Private _dpiFactor As Single = 1.0F

  Public Sub New()
    Me.InitializeComponent()
    Me._dpiFactor = Me.AutoScaleDimensions.Height / 192.0F
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemMenu.Text = Language.GetLanguageString("Menu")
    Me.MenuItemClear.Text = Language.GetLanguageString("Clear")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
    Me.OnResize(EventArgs.Empty)
  End Sub

  Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    Dim topPad As Integer = 70 * Me._dpiFactor
    Dim btPad As Integer = 6 * Me._dpiFactor
    Dim xSize As Integer = (Me.Width - (4 * btPad)) / 3
    Dim ysize As Integer = (Me.Height - (topPad + (5 * btPad))) / 4

    Me.LabelTop.Size = New Size(Me.Width, 50 * Me._dpiFactor)
    Me.LabelTop.Location = New Point(0, (topPad - Me.LabelTop.Height) >> 1)

    Me.WP7Button7.Location = New Point(btPad, topPad + btPad)
    Me.WP7Button7.Size = New Size(xSize, ysize)

    Me.WP7Button8.Location = New Point((2 * btPad) + xSize, topPad + btPad)
    Me.WP7Button8.Size = New Size(xSize, ysize)

    Me.WP7Button9.Location = New Point((3 * btPad) + (2 * xSize), topPad + btPad)
    Me.WP7Button9.Size = New Size(xSize, ysize)

    Me.WP7Button4.Location = New Point(btPad, topPad + (2 * btPad) + ysize)
    Me.WP7Button4.Size = New Size(xSize, ysize)

    Me.WP7Button5.Location = New Point((2 * btPad) + xSize, topPad + (2 * btPad) + ysize)
    Me.WP7Button5.Size = New Size(xSize, ysize)

    Me.WP7Button6.Location = New Point((3 * btPad) + (2 * xSize), topPad + (2 * btPad) + ysize)
    Me.WP7Button6.Size = New Size(xSize, ysize)

    Me.WP7Button1.Location = New Point(btPad, topPad + (3 * btPad) + (2 * ysize))
    Me.WP7Button1.Size = New Size(xSize, ysize)

    Me.WP7Button2.Location = New Point((2 * btPad) + xSize, topPad + (3 * btPad) + (2 * ysize))
    Me.WP7Button2.Size = New Size(xSize, ysize)

    Me.WP7Button3.Location = New Point((3 * btPad) + (2 * xSize), topPad + (3 * btPad) + (2 * ysize))
    Me.WP7Button3.Size = New Size(xSize, ysize)

    Me.WP7ButtonPM.Location = New Point(btPad, topPad + (4 * btPad) + (3 * ysize))
    Me.WP7ButtonPM.Size = New Size(xSize, ysize)

    Me.WP7Button0.Location = New Point((2 * btPad) + xSize, topPad + (4 * btPad) + (3 * ysize))
    Me.WP7Button0.Size = New Size(xSize, ysize)

    Me.WP7ButtonBack.Location = New Point((3 * btPad) + (2 * xSize), topPad + (4 * btPad) + (3 * ysize))
    Me.WP7ButtonBack.Size = New Size(xSize, ysize)

    MyBase.OnResize(e)
  End Sub

  Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    Select Case e.KeyChar
      Case "0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c
        Me.LabelTop.Text &= e.KeyChar.ToString
        Me.ValidateText()
        e.Handled = True
      Case "-"c, "+"c
        Me.WP7ButtonPM_Click(Me, EventArgs.Empty)
        e.Handled = True
      Case vbBack
        Me.WP7ButtonBack_Click(Me, EventArgs.Empty)
        e.Handled = True
    End Select

    MyBase.OnKeyPress(e)
  End Sub

  Public Overrides Property Text() As String
    Get
      Return Me.LabelTop.Text
    End Get
    Set(ByVal value As String)
      Me.LabelTop.Text = value
      Me.ValidateText()
    End Set
  End Property

  Public Property ValueInteger() As Integer
    Get
      Try
        Dim d As Decimal = Decimal.Parse(Me.LabelTop.Text)
        If d > Integer.MaxValue Then
          Return Integer.MaxValue
        ElseIf d < Integer.MinValue Then
          Return Integer.MinValue
        End If
        Return Integer.Parse(Me.LabelTop.Text)
      Catch
        Me.ValidateText()
        Return 0
      End Try
    End Get
    Set(ByVal value As Integer)
      Me.LabelTop.Text = value.ToString
    End Set
  End Property

  Public Property ValueDecimal() As Decimal
    Get
      Try
        Return Decimal.Parse(Me.LabelTop.Text)
      Catch
        Me.ValidateText()
        Return 0
      End Try
    End Get
    Set(ByVal value As Decimal)
      Me.LabelTop.Text = value.ToString
    End Set
  End Property

  Private Sub WP7Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button7.MouseDown
    Me.LabelTop.Text &= "7"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button8.MouseDown
    Me.LabelTop.Text &= "8"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button9.MouseDown
    Me.LabelTop.Text &= "9"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button4.MouseDown
    Me.LabelTop.Text &= "4"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button5.MouseDown
    Me.LabelTop.Text &= "5"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button6.MouseDown
    Me.LabelTop.Text &= "6"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button1.MouseDown
    Me.LabelTop.Text &= "1"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button2.MouseDown
    Me.LabelTop.Text &= "2"
    Me.ValidateText()
  End Sub

  Private Sub WP7Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button3.MouseDown
    Me.LabelTop.Text &= "3"
    Me.ValidateText()
  End Sub

  Private Sub WP7ButtonPM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7ButtonPM.MouseDown
    Try
      Me.LabelTop.Text = (0 - Decimal.Parse(Me.LabelTop.Text)).ToString
    Catch
      Me.LabelTop.Text = "0"
    End Try

    Me.ValidateText()
  End Sub

  Private Sub WP7Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7Button0.MouseDown
    Me.LabelTop.Text &= "0"
    Me.ValidateText()
  End Sub

  Private Sub WP7ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WP7ButtonBack.MouseDown
    Me.LabelTop.Text = Microsoft.VisualBasic.Left(Me.LabelTop.Text, Me.LabelTop.Text.Length - 1)
    Me.ValidateText()
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub MenuItemClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemClear.Click
    Me.LabelTop.Text = "0"
    Me.ValidateText()
  End Sub

  Private Sub ValidateText()
    ' Check for empty string
    If Me.LabelTop.Text.Length <= 0 Then
      Me.LabelTop.Text = "0"
    End If

    ' Check for string that is too long
    If Me.LabelTop.Text.Length > 18 Then
      Me.LabelTop.Text = Microsoft.VisualBasic.Left(Me.LabelTop.Text, 18)
    End If

    Try
      Me.LabelTop.Text = Decimal.Parse(Me.LabelTop.Text).ToString
    Catch
      Me.LabelTop.Text = "0"
    End Try
  End Sub
End Class
